﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using vensoft.BussinesLogic;
using vensoft.Entities;

namespace vensoft.Win32
{
    public partial class frmGestionarPersonal : Form
    {
        IPersonalBL personalBL;

        public frmGestionarPersonal()
        {
            InitializeComponent();
        }

        public Int32 estadoBotones { get; set; }

        private void frmGestionarPersonal_Load(object sender, EventArgs e)
        {
            personalBL = new PersonalBL();
           

            if (estadoBotones == 0)
            {
                personalBindingSource.DataSource = personalBL.RetornarPersonalSinUsuario();
                btnSeleccionar.Visible = true;
                btnNuevo.Visible = false;
                btnEditar.Visible = false;
                btnEliminar.Visible = false;
            }
            else
            {
                personalBindingSource.DataSource = personalBL.GetAllFromPersonal();
                btnSeleccionar.Visible = false;
                btnNuevo.Visible = true;
                btnEditar.Visible = true;
                btnEliminar.Visible = true;
            }
        }

        private void txtBuscarPer_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                personalBL = new PersonalBL();
                if (estadoBotones == 0)
                {
                    personalBindingSource.DataSource = personalBL.GetFromPersonalByDescNoAct(txtBuscarPersonal.Text);
                }
                else
                personalBindingSource.DataSource = personalBL.GetFromPersonalByDescripcion(txtBuscarPersonal.Text);
            }
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            frmAEPersonal aepersonal = new frmAEPersonal();
            aepersonal.CurrentPers = personalBindingSource.Current as Personal;
            aepersonal.Editar();
            aepersonal.ShowDialog();
            personalBindingSource.DataSource = personalBL.GetAllFromPersonal();  
        }

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            frmAEPersonal aepersonal = new frmAEPersonal();
            aepersonal.Nuevo();
            aepersonal.ShowDialog();
            personalBindingSource.DataSource = personalBL.GetAllFromPersonal();
        }

        

        private void btnCancelar_Click(object sender, EventArgs e)
        {            
            this.Close();
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("¿Estas seguro que deseas eliminar?", "ALERTA", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Personal currentPersonal = personalBindingSource.Current as Personal;
                personalBL.EliminarPersonal(currentPersonal);
                personalBindingSource.RemoveCurrent();
            }
        }

        private void btnActualizarDatos_Click(object sender, EventArgs e)
        {
            personalBL = new PersonalBL();
            if (estadoBotones == 0)
            {
                personalBindingSource.DataSource = personalBL.RetornarPersonalSinUsuario();  
            }
            else
            {
                personalBindingSource.DataSource = personalBL.GetAllFromPersonal();
            }          
        }

        private void btnSeleccionar_Click(object sender, EventArgs e)
        {
            //CurrentPers = personalBindingSource.Current as Personal;
            //this.Close();
            Personal personalSelect = personalBindingSource.Current as Personal;
            if (personalSelect != null)
            {
                personal = personalSelect;
            }
            Close();
        }

        //public Personal CurrentPers { get; set; }
        public Personal personal { get; set; }
    }
}
